package com.joe.blockview.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.joe.blockview.R;

public class DialogUtils {

    public static void showEditDialog(final Context context, String title, final DialogConfireListener listener) {
        final Dialog dialog = new Dialog(context);
        View v = LayoutInflater.from(context).inflate(R.layout.layout_dialog_edit, null);
        final EditText editText = v.findViewById(R.id.dialog_edit_ed);
        TextView tv = v.findViewById(R.id.dialog_edit_title);
        tv.setText(title);
        v.findViewById(R.id.dialog_edit_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        v.findViewById(R.id.dialog_edit_confire).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editText.getText().toString().isEmpty()) {
                    Toast.makeText(context, "输入内容不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    dialog.dismiss();
                    if (listener != null) {
                        listener.onConfire(editText.getText().toString());
                    }
                }

            }
        });
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dialog.addContentView(v, layoutParams);
        dialog.show();
    }

    public static void showNotifyDialog(final Context context, String title, String ct, final DialogConfireListener listener) {
        final Dialog dialog = new Dialog(context);
        View v = LayoutInflater.from(context).inflate(R.layout.layout_dialog_notify, null);
        final TextView content = v.findViewById(R.id.dialog_notify_content);
        content.setText(ct);
        TextView tv = v.findViewById(R.id.dialog_notify_title);
        tv.setText(title);
        v.findViewById(R.id.dialog_notify_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        v.findViewById(R.id.dialog_notify_confire).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
                if (listener != null) {
                    listener.onConfire("");
                }
            }
        });
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        dialog.addContentView(v, layoutParams);
        dialog.show();
    }

    public interface DialogConfireListener {
        public void onConfire(String name);
    }
}
